//
//  QianliLuanchProc.swift
//  todoios
//
//  Created by qianli on 2025/4/11.
//

import Foundation
import UIKit


class QianliLuanchProc: NSObject {
    //MARK: - 启动过程View
    class func kpUusJz0peR6xwJn(){
        let qianliMainVC = QianMainWindowVC()
        qianliMainVC.preInitView()

        let appearView:QianliAppearView? = QianliAppearView(frame: CGRect(x: 0, y: 0, width: qianli_pm_width, height: qianli_pm_height))
        // 使用弱引用避免循环引用
        appearView?.tapBlock = { [weak appearView] in
            if let adView = appearView{
                UIView.animate(withDuration: 0.5, animations: {
                    adView.alpha = 0.0
                }, completion: { _ in
                    for perView in adView.subviews {
                        perView.removeFromSuperview()
                    }
                    adView.removeFromSuperview()
                    self.fDP9XPtmznJ8jC4q(to: qianliMainVC)
                })
            }else{
                self.fDP9XPtmznJ8jC4q(to: qianliMainVC)
            }
        }
        
        let scene = UIApplication.shared.connectedScenes.first
        guard let windowScene = scene as? UIWindowScene else  {return}
        if let window = windowScene.windows.filter({$0.isKeyWindow}).first{
            if let adView = appearView{
                window.addSubview(adView)
            }
        }
         
    }
    
    private class func fDP9XPtmznJ8jC4q(to viewController: UIViewController) {
        DispatchQueue.main.async {
            let scene = UIApplication.shared.connectedScenes.first
            guard let windowScene = scene as? UIWindowScene else { return }
            if let window = windowScene.windows.first {
                //取window方式一：
                window.rootViewController = viewController
            } else if let sceneDelegate = UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate {
                //方式二：也可以直接引用SceneDelegate的对象window
                sceneDelegate.window?.rootViewController = viewController
            }
        }
    }
    
}
